<?php
class store extends spController
{
	function __construct()
	{
	        // 必须加入启动父类构造函数的操作
	        parent::__construct();
	         
	        // 开始全局操作
	        $this->arr_cat = Store_Cat();
			$this->a = $_GET['a'];
			$this->c = $_GET['c'];
	}

	function main()
	{
		$m_store = spClass("m_store");
		$this->results = $m_store->spPager($this->spArgs('page', 1), 10)->findAll(array('isdel'=>0), 'type');
		$this->pager = $m_store->spPager()->getPager();
		$this->display("admin/store_list.html");
	}

	function mini()
	{
		$m_store = spClass("m_store");
		$this->results = $m_store->spPager($this->spArgs('page', 1), 60)->findAll(array('isdel'=>0), 'type');
		$this->pager = $m_store->spPager()->getPager();
		$this->display("admin/store_mini_list.html");
	}

	function mini_search()
	{
		$m_store = spClass("m_store");
		$this->catid = $catid = intval($_GET['catid']);
		$this->kword = $kword = $_GET['k'];
		$this->store_level = $_GET['store_level'];
	
		$condition = 'isdel=0';
		if($catid) {			
			$condition .= " AND catid='$catid'";
		}
		if($kword && $kword != '店铺名称或店主名称') {
			$condition .= " AND store_name LIKE '%$kword%' OR nickname LIKE '%$kword%'";
		}
		if($this->store_level) {
			$condition .= " AND store_level='$this->store_level'";
		}
		$this->results = $m_store->spPager($this->spArgs('page', 1), 60)->findAll($condition, 'type');
		$this->pager = $m_store->spPager()->getPager();
		$this->display("admin/store_mini_list.html");
	}

	function search()
	{
		$m_store = spClass("m_store");
		$this->catid = $catid = intval($_GET['catid']);
		$this->kword = $kword = $_GET['k'];
		$this->level = $_GET['level'];

		$condition = 'isdel=0';
		if($catid) {			
			$condition .= " AND catid='$catid'";
		}
		if($kword && $kword != '店铺名称或店主名称') {
			$condition .= " AND store_name LIKE '%$kword%' OR nickname LIKE '%$kword%'";
		}
		if($this->level) {
			$condition .= " AND level='$level'";
		}
		$this->results = $m_store->spPager($this->spArgs('page', 1), 10)->findAll($condition, 'type');
		$this->pager = $m_store->spPager()->getPager();
		$this->display("admin/store_list.html");
	}

	function add()
	{
		$m_store = spClass("m_store");
		if(!empty($_POST)) {
			$verifier = $m_store->spVerifier($_POST);
			if( false != $verifier) {
				$this->error('提交数据不正确', spUrl('store', 'add'));
			} else {
				if($_POST['catid'] <= 0) {
					$this->error('分类数据不正确', spUrl('store', 'add'));
				}
				if($m_store->find(array('store_id'=>$_POST['store_id']))) {
					$this->error('店铺已经添加', spUrl('store', 'add'));
				}
				$m_store->create($_POST);
				$this->success('提交成功', spUrl('store', 'main'));
			}
		} else {
			$this->arr_cid = spAccess('r',  'arr_cid');	// 类目缓存
			$this->display("admin/store_add.html");
		}
	}

	function edit()
	{
		$m_store = spClass("m_store");
		if(!empty($_POST)) {
			$verifier = $m_store->spVerifier($_POST);
			if( false != $verifier) {
				$this->error('提交数据不正确', spUrl('store', 'edit', array('store_id'=>$_POST['store_id'])));
			} else {
				if($_POST['catid'] <= 0) {
					$this->error('提交数据不正确', spUrl('store', 'edit', array('store_id'=>$_POST['store_id'])));
				}
				$store_id = $_POST['store_id'];
				unset($_POST['store_id']);
				$m_store->update(array('store_id'=>$store_id), $_POST);
				$this->success('修改成功', spUrl('store', 'main'));
			}
		} else {
			$store_id = $_GET['store_id'];
			$this->result = $m_store->find(array('store_id'=>$store_id));
			$this->arr_cid = spAccess('r',  'arr_cid');	// 类目缓存
			$this->display("admin/store_edit.html");
		}
	}

	function del()
	{
		$m_store = spClass("m_store");
		$store_id = intval($_GET['store_id']);
		$m_store->update(array('store_id'=>$store_id), array('isdel'=>1));
		$this->success('删除成功', spUrl('store', 'main'));
	}

	function getAjax_sidAndAlimamaurl()
	{
		$store_name = trim($_GET['store_name']);
		if(!empty($store_name)) {
			$m_taobao = spClass('m_taobao');
			$shopgetreq = new ShopGetRequest;
			$shopgetreq->setFields("title,sid,cid");
			$shopgetreq->setNick($store_name);
			$resp = $m_taobao->m_topsdk->execute($shopgetreq);
			if($resp->code === 560) {
				exit('-2');
			}
			$m_store = spClass("m_store");
			if(($_GET['store_id'] && $resp->shop->sid !=$_GET['store_id']) || empty($_GET['store_id']) ) {
				if($m_store->find(array('store_id'=>$resp->shop->sid))) {
					exit('-1');
				}
			}
			$tbshopconvert = new TaobaokeShopsConvertRequest;
			
			$tbshopconvert->setFields("click_url,shop_type");
			$tbshopconvert->setSids($resp->shop->sid);
			$tbshopconvert->setNick("半价汇");
			//$tbshopconvert->setPid('mm_29938918_0_0'); //总是说报错，去提BUG //2012-2-3  已解决，添加中间的数字即可
			
			$convert_resp = $m_taobao->m_topsdk->execute($tbshopconvert);

			$result = array('title'=>$resp->shop->title, 'sid'=>$resp->shop->sid, 'alimama_url'=>$convert_resp->taobaoke_shops->taobaoke_shop[0]->click_url, 'type'=>$convert_resp->taobaoke_shops->taobaoke_shop[0]->shop_type, 'cid'=>$resp->shop->cid);
			exit(json_encode($result));
		}
		return false;
	}

	function store_cid()
	{
		$m_taobao = spClass('m_taobao');
		$req = new ShopcatsListGetRequest;
		$req->setFields("cid,parent_cid,name,is_parent");
		$resp = $m_taobao->m_topsdk->execute($req);
		$arr_cats = $resp->shop_cats->shop_cat;
		foreach($arr_cats as $k=>$v)
		{
			$result[$v->cid] = $v->name;
		}
		spAccess('w',  'arr_cid', $result, 2419200);
	}

	function update_store_cid()
	{
		$this->startnum = $startnum = $_GET['startnum'] ? $_GET['startnum'] : 0;
		$sql = "SELECT store_id, nickname FROM store WHERE cid=0 ORDER BY store_id ASC LIMIT $startnum, 20";
	
		$m_store = spClass("m_store");
		$result = $m_store->findSql($sql);
		foreach($result as $k=>$v) {
			$store_id = $v['store_id'];
			$m_taobao = spClass('m_taobao');
			$shopgetreq = new ShopGetRequest;
			$shopgetreq->setFields("title,sid,cid");
			$shopgetreq->setNick(trim($v['nickname']));
			$resp = $m_taobao->m_topsdk->execute($shopgetreq);
			$cid = $resp->shop->cid;
			$m_store->update(array('store_id'=>$store_id), array('cid'=>$cid, 'nickname'=>trim($v['nickname'])));
			unset($m_taobao, $resp, $shopgetreq);
		}
		$this->startnum = $this->startnum + 20;
		$this->display("update_type.html");
	}
}